package com.wtwd.wtwddevice.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.util.DateUtils;
import com.wtwd.wtwddevice.mapper.DeviceHeartRateMapper;
import com.wtwd.wtwddevice.model.entity.DeviceHeartRate;
import com.wtwd.wtwddevice.model.entity.DeviceHeartRateTotal;
import com.wtwd.wtwddevice.service.IDeviceHeartRateService;
import com.wtwd.wtwddevice.service.IDeviceHeartRateTotalService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IRedisService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-05-16
 */
@Service
public class DeviceHeartRateServiceImpl extends ServiceImpl<DeviceHeartRateMapper, DeviceHeartRate> implements IDeviceHeartRateService {


    @Resource
    private IDeviceHeartRateTotalService deviceHeartRateTotalService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(DeviceHeartRate heartRateData) {
        DeviceHeartRateTotal heartRateTotal = getSynchronizationCache(heartRateData);
        heartRateData.setDeviceHeartRateTotalId(heartRateTotal.getId());
        boolean save = save(heartRateData);
        if (save){
            heartRateTotal.setLastInfoId(heartRateData.getId());
            deviceHeartRateTotalService.updateById(heartRateTotal);
        }
        return save;
    }

    @Override
    public DeviceHeartRateTotal getSynchronizationCache(DeviceHeartRate heartRateData) {
        DeviceHeartRateTotal heartRateCache = deviceHeartRateTotalService.getDeviceHeartRateTotal(heartRateData.getDeviceId(), DateUtils.convertDateFormat(heartRateData.getTime()));
        if (heartRateCache != null) {
            heartRateCache.setMin(heartRateCache.getMin() > heartRateData.getHeartRate() ? heartRateData.getHeartRate() : heartRateCache.getMin());
            heartRateCache.setMax(heartRateCache.getMax() < heartRateData.getHeartRate() ? heartRateData.getHeartRate() : heartRateCache.getMax());
            heartRateCache.setTotal(heartRateCache.getTotal() == null ? heartRateData.getHeartRate() : (heartRateCache.getTotal() + heartRateData.getHeartRate()));
            heartRateCache.setRows(heartRateCache.getRows() == null ? 1 : heartRateCache.getRows() + 1);
        } else {
            heartRateCache = new DeviceHeartRateTotal();
            BeanUtils.copyProperties(heartRateData, heartRateCache);
            heartRateCache.setMin(heartRateData.getHeartRate());
            heartRateCache.setMax(heartRateData.getHeartRate());
            heartRateCache.setTotal(heartRateData.getHeartRate());
            heartRateCache.setRows(1);
            heartRateCache.setTime(DateUtils.convertDateFormat(heartRateData.getTime()));
            deviceHeartRateTotalService.save(heartRateCache);
        }
        return heartRateCache;
    }
}
